﻿<!DOCTYPE HTML>
<html>
    <head>
        <title>JavaScript Extension: Performance</title>
        <style>
            body { font-family: Tahoma, Serif; font-size: 9pt; }
        </style>
    </head>
    <body>
        <h1>JavaScript Extension: Performance</h1>

        <div><span id="statusBox"></span> <progress id="progressBox" value="0" style="display:none"></progress></div>

        <div style="padding-top:10px; padding-bottom:10px">
        <table id="resultTable" border="1" cellspacing="1" cellpadding="4" width="100%">
            <thead>
                <tr>
                    <td>Name</td>
                    <td>Min</td>
                    <td>Avg</td>
                    <td>Max</td>
                    <td>Probes</td>
                </tr>
            </thead>
            <!-- result rows here -->
        </table>
        </div>

<script type="text/javascript">
(function () {
    var asyncExecution = true;
    var testIterations = 100000;
    var totalProbes = 10;
    var probeDelay = 0;

    var collectProbes = false;

    function dummyCallTest() {
        for (var i = 0; i < testIterations; i++) {
            cef.test.dummy();
        }
    }

    function execTestFunc(func) {
        var begin = new Date();
        func();
        var end = new Date();
        return (end - begin);
    }

    function execTest(test) {
        function nextStep() {
            if (asyncExecution) {
                setTimeout(function () { execTest(test); }, probeDelay);
            } else {
                execTest(test);
            }
        }

        function nextTest() {
            appendResult(test);
            // ...
        }

        updateStatus(test);
        if (!test.warmedUp) {
            execTestFunc(test.func);
            test.warmedUp = true;
            return nextStep();
        }

        if (test.probe >= test.totalProbes) {
            test.avg = test.total / test.totalProbes;
            return nextTest();
        }

        var elapsed = execTestFunc(test.func);
        test.total += elapsed;
        if (!test.min) test.min = elapsed;
        else if (test.min > elapsed) test.min = elapsed;
        if (!test.max) test.max = elapsed;
        else if (test.max < elapsed) test.max = elapsed;
        if (collectProbes) {
            test.results.push(elapsed);
        }
        test.probe++;

        return nextStep();
    }

    function updateStatus(test) {
        var statusBox = document.getElementById("statusBox");
        var progressBox = document.getElementById("progressBox");

        if (test.probe >= test.totalProbes) {
            statusBox.innerText = test.name + " completed.";
            progressBox.style.display = 'none';
        } else {
            statusBox.innerText = test.name + " (" + test.probe + "/" + test.totalProbes + ")";
            progressBox.value = (test.probe / test.totalProbes);
            progressBox.style.display = 'inline';
        }
    }

    function appendResult(test) {
        var e = document.getElementById("resultTable");

        e.insertAdjacentHTML("beforeEnd",
                            ["<tr>",
                             "<td>", test.name, "</td>",
                             "<td>", test.min, "ms</td>",
                             "<td>", test.avg, "ms</td>",
                             "<td>", test.max, "ms</td>",
                             "<td>", test.results.join(", "), "</td>",
                             "<tr>"
                             ].join("")
                             );
    }

    function runTest(name, func) {
        var test = { name: name, func: func, warmedUp: false, total: 0, totalProbes: totalProbes, probe: 0, results: [] };
        setTimeout(function () { execTest(test); }, 10);
    }

    runTest("dummyCall", dummyCallTest);

})();
</script>

    </body>
</html>
